package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityDomainBinding;
import stark.common.basic.device.NetUtil;
import stark.common.basic.inf.IRetCallback;

public class DomainActivity extends BaseAc<ActivityDomainBinding> {

    private String host;

    @Override
    protected int onCreate() {
        return R.layout.activity_domain;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDomainBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvQuery.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvQuery://查询
                handleClickParseDns();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void handleClickParseDns() {
        host = mDataBinding.etHost.getText().toString().trim();
        if (TextUtils.isEmpty(host)) {
            ToastUtils.showShort(getString(R.string.please_input_domain));
            return;
        }
        NetUtil.getAllHostAddressByName(host, new IRetCallback<List<String>>() {
            @Override
            public void onResult(List<String> result) {
                StringBuilder builder = new StringBuilder();
                if (result != null && result.size() > 0) {
                    for (String address : result) {
                        builder.append(host + "/" + address + "\n");
                    }
                } else {
                    builder.append(R.string.no_dns);
                }
                mDataBinding.tvResult.setText(builder.toString());
                mDataBinding.llResult.setVisibility(View.VISIBLE);
            }
        });
    }

}